<template>
  <div class="employees-container">
    <div class="app-container">
      <el-card>
        <!-- 放置表单 -->
        <el-form ref="userForm" label-width="120px" :model="userInfo" :rules="rules" style="margin-left: 120px; margin-top:30px">
          <el-form-item label="姓名:" prop="username">
            <el-input v-model="userInfo.username" style="width:300px" />
          </el-form-item>

          <el-form-item label="手机">
            <el-input v-model="userInfo.mobile" disabled style="width:300px" />
          </el-form-item>

          <el-form-item label="入职时间">
            <el-date-picker
              v-model="userInfo.timeOfEntry"
              type="date"
              value-format="yyyy-MM-dd"
            />
          </el-form-item>

          <el-form-item label="员工头像">
            <!--
              方案一
              缺点
              ref必须知道子组件的属性名，才可以 -->
            <!-- <upload-img ref="img" /> -->
            <upload-img v-model="userInfo.staffPhoto" />
          </el-form-item>

          <!-- 保存个人信息 -->
          <el-row class="inline-info" type="flex" justify="center">
            <el-col :span="12">
              <el-button type="primary" @click="confirmFn">保存更新</el-button>
              <el-button @click="$router.back()">返回</el-button>
            </el-col>
          </el-row>
        </el-form>
      </el-card>
    </div>
  </div>
</template>
<script>
import { getUserDetailById } from '@/api/user'
import { saveUserDetailById } from '@/api/employees'
export default {
  name: 'Detail',
  data() {
    return {
      userInfo: {
        // username: '',
        // mobile: '',
        // timeOfEntry: '',
        // staffPhoto: ''

      },
      rules: {
        username: [
          { required: true, message: '姓名不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    this.hGetUserInfoById()
  },
  methods: {
    // 获取回显请求数据
    async hGetUserInfoById() {
      const res = await getUserDetailById(this.$route.query.id)// 路由取值
      this.userInfo = res.data
      // 把图片回显
      // this.$refs.img.imageUrl = this.userInfo.staffPhoto
    },
    // 重点在这里哦
    hSaveUserDetailById() {
      this.$refs.userForm.validate(async valid => {
        // console.log('子组件的图片地址', this.$refs.img.imageUrl)
        // 使用最新修改数据覆盖源数据 作为接口提交数据,取数据，记得这个接口要的是对象
        // await saveUserDetailById({ ...this.userInfo, ...{ staffPhoto: this.$refs.img.imageUrl }})
        await saveUserDetailById(this.userInfo)
        this.$message.success('更新成功')
        // this.$router.back()//可以让他自动返回
      })
    },
    confirmFn() {
      this.hSaveUserDetailById()
    }
  }
}
</script>
